using System.Collections;
using System.Collections.Generic;
using DMS.BEs;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using ObjectFactories;

namespace DMS.NH.Testing
{
	[TestFixture]
	public class ChinookGetAll : ChinookTestBase
	{
		private ISessionFactory sessionFactory;

		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			Configuration cfg = GetConfiguration();

			SchemaExport se = new SchemaExport(cfg);
			se.Create(false, true);

			sessionFactory = cfg.BuildSessionFactory();

			using (ISession session = sessionFactory.OpenSession())
			using (ITransaction transaction = session.BeginTransaction())
			{
				SaveAll(session, AlbumFactory.GetAll());
				SaveAll(session, ArtistFactory.GetAll());
				SaveAll(session, CustomerFactory.GetAll());
				SaveAll(session, EmployeeFactory.GetAll());
				SaveAll(session, GenreFactory.GetAll());
				SaveAll(session, InvoiceFactory.GetAll());
				SaveAll(session, MediaTypeFactory.GetAll());
				SaveAll(session, PlaylistFactory.GetAll());

				transaction.Commit();
			}
		}

		private static void SaveAll(ISession session, IEnumerable entities)
		{
			foreach (var e in entities)
				if (e != null)
					session.Save(e);
		}

		private void CheckGetAll<T>(int count) where T : class
		{
			using (ISession session = sessionFactory.OpenSession())
			{
				IList<T> list = session.CreateCriteria<T>().List<T>();
				Assert.AreEqual(count, list.Count);
			}
		}

		[Test]
		public void AlbumGetAllWithHQLTest()
		{
			CheckGetAll<Album>(347);
		}

		[Test]
		public void ArtistGetAllWithHQLTest()
		{
			CheckGetAll<Artist>(275);
		}

		[Test]
		public void CustomerGetAllWithHQLTest()
		{
			CheckGetAll<Customer>(59);
		}

		[Test]
		public void EmployeeGetAllWithHQLTest()
		{
			CheckGetAll<Employee>(8);
		}

		[Test]
		public void GenreGetAllWithHQLTest()
		{
			CheckGetAll<Genre>(25);
		}

		[Test]
		public void InvoiceGetAllWithHQLTest()
		{
			CheckGetAll<Invoice>(458);
		}

		[Test]
		public void MediaTypeGetAllWithHQLTest()
		{
			CheckGetAll<MediaType>(5);
		}

		[Test]
		public void PlaylistGetAllWithHQLTest()
		{
			CheckGetAll<Playlist>(18);
		}

		[Test]
		public void TrackGetAllWithHQLTest()
		{
			CheckGetAll<Track>(3503);
		}
	}
}